// import Vue from 'vue'

export default {
  namespaced: true,
  state: {
    cartData: localStorage.getItem('cartData') ? JSON.parse(localStorage.getItem('cartData')) : []
  },
  mutations: {
    // 添加商品到购物车
    ADD_ITEM (state, payload) {
      let isSame = false
      if (state.cartData.length > 0) {
        for (let i = 0; i < state.cartData.length; i++) {
          if (payload.cartData.gid === state.cartData[i].gid && JSON.stringify(payload.cartData.attrs) === JSON.stringify(state.cartData[i].attrs)) {
            isSame = true
            state.cartData[i].amount = parseInt(state.cartData[i].amount) + parseInt(payload.cartData.amount)
          }
        }
      }
      if (!isSame) {
        state.cartData.push(payload.cartData)
      }
      localStorage.setItem('cartData', JSON.stringify(state.cartData))
    },
    // 删除商品
    DEL_ITEM (state, payload) {
      state.cartData.splice(payload.index, 1)
      localStorage.setItem('cartData', JSON.stringify(state.cartData))
    },
    // 增加商品数量
    INC_AMOUNT (state, payload) {
      if (state.cartData) {
        state.cartData[payload.index].amount += 1
        localStorage.setItem('cartData', JSON.stringify(state.cartData))
      }
    },
    // 设置商品数量
    SET_AMOUNT (state, payload) {
      state.cartData[payload.index].amount = payload.amount
      state.cartData[payload.index].amount = parseInt(state.cartData[payload.index].amount.replace(/[^\d]/g, ''))
      if (!state.cartData[payload.index].amount) {
        state.cartData[payload.index].amount = 1
      }
      localStorage['cartData'] = JSON.stringify(state.cartData)
    },
    // 减少商品数量
    DEC_AMOUNT (state, payload) {
      if (state.cartData) {
        state.cartData[payload.index].amount = state.cartData[payload.index].amount > 1 ? state.cartData[payload.index].amount - 1 : 1
        localStorage.setItem('cartData', JSON.stringify(state.cartData))
      }
    },
    // 选择商品
    SELECT_ITEM (state, payload) {
      if (state.cartData) {
        state.cartData[payload.index].checked = !state.cartData[payload.index].checked
        // Vue.set(state.cartData, payload.index, state.cartData[payload.index])
        localStorage.setItem('cartData', JSON.stringify(state.cartData))
      }
    },
    //全选/反选
    ['ALL_SELECT_ITEM'] (state, payload) {
      if (state.cartData.length > 0) {
        for (let i = 0; i < state.cartData.length; i++) {
          state.cartData[i].checked = payload.checked
        }
        localStorage['cartData'] = JSON.stringify(state.cartData)
      }
    },

    // 删除已经提交订单的商品
    DEL_ORDER_DATA (state, payload) {
      if (state.cartData.length > 0) {
        payload.orderData.forEach((item) => {
          state.cartData.forEach((cart, index) => {
            if (item.gid === cart.gid && JSON.stringify(item.attrs) === JSON.stringify(cart.attrs)) {
              state.cartData.splice(index, 1)
            }
          })
        })
        localStorage['cartData'] = JSON.stringify(state.cartData)
      }
    }
  },
  getters: {
    // 计算总金额
    total (state) {
      let total = 0
      if (state.cartData.length > 0) {
        state.cartData.forEach((item) => {
          if (item.checked) {
            total += item.price * item.amount
          }
        })
        return parseFloat(total.toFixed(2))
      } else {
        return 0
      }
    },
    // 计算运费
    freight (state) {
      let freight = []
      if (state.cartData.length > 0) {
        state.cartData.forEach((item) => {
          if (item.checked) {
            freight.push(item.freight)
          }
        })
        return freight.length > 0 ? Math.max(...freight) : 0
        // return freight.length > 0 ? Math.max.apply(null, freight) : 0
      } else {
        return 0
      }
    }

  }
}
